<?php





namespace app\admin\controller;





use app\admin\model\PersonModel;

use app\BaseController;

use app\common\Constants\ErrorCode;

use app\common\traits\ActionRecordTrait;

use think\response\Json;



class Person extends  ApiCommon

{

    use ActionRecordTrait;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @author Mr.Li

     * @create 2021-05-26 16:20

     */

    public function initialize()

    {

        $action = [

            'permission' => ['exportcsv','import'],

            'allow' => [

                'synckingdeestock'

            ]

        ];

        event('CheckAuth', $action);

        $request = $this->request;

        $a       = strtolower($request->action());

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    /**

     * 创建店铺员工资源

     * @author ran

     * @date 2021-09-16 14:12

     * mailbox 466180170@qq.com

     */

    public function createAdminShopPersonResource():Json

    {

        $request = $this->request->param();

        $model = new PersonModel();

        $res          = $model->createPersonResource($request);

        if (!$res) {

            return  $this->failed(ErrorCode::SYSTEM_INVALID,$model->getError());

        }

        return  $this->success([],'创建成功');

    }

    /**

     * 获取店铺员工资源

     * @author ran

     * @date 2021-09-16 14:12

     * mailbox 466180170@qq.com

     */

    public function readAdminShopPersonList():Json

    {

        $request = $this->request->param();

        $model = new PersonModel();

        $shopIds = $this->getCommonShopIdsCollectByUserType();

        if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);

        $res          = $model->getPersonAdminListResource($shopIds,(int)$request['page'],(int)$request['limit'],$request);

        return $this->success($res,'获取资源成功',$res['dataCount']);

    }

     /**

     * 销售出库单获取开单人员工

     * @author ran

     * @date 2021-09-16 14:12

     * mailbox 466180170@qq.com

     */

    public function readAdminShopPersonListByRefundOrder():Json

    {

        $request = $this->request->param();

        $shopId = $this->request->param('shopId');

        $shopId =!empty($this->userInfo['shop_id'])?$this->userInfo['shop_id']:$shopId;

        $model = new PersonModel();

        if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);

        $res          = $model->getShopPersonListByRefundOrder($shopId,(int)$request['page'],(int)$request['limit'],$request);

        return $this->success($res,'获取资源成功',$res['dataCount']);

    }

    /**

     * 获取店铺员工详情

     * @author ran

     * @date 2021-09-16 14:12

     * mailbox 466180170@qq.com

     */

    public function readAdminPersonInfo():Json

    {

        $id = $this->request->param('id');

        if (empty($id)) return $this->failed(ErrorCode::SYSTEM_INVALID);

        $model = new PersonModel();

        $res          = $model->getPersonAdminInfoResource($id);

        return $this->success($res,'获取成功');

    }



    /**

     * 更新

     * @author ran

     * @date 2021-09-25 15:47

     * mailbox 466180170@qq.com

     */

    public function updateAdminShopResource(){

        $request = $this->request->param();

        $model = new PersonModel();

        $res          = $model->updatePersonAdminResource($request);

        if (!$res) {

            return  $this->failed(ErrorCode::SYSTEM_INVALID,$model->getError());

        }

        return  $this->success([],'更新成功');

    }



    /**

     * 导出店铺资源信息

     * @return Json

     * @author ran

     * @date 2021-12-01 12:53

     * mailbox 466180170@qq.com

     */

    public function exportAdminPersonList()

    {

        $request = $this->request->param();

        $shopIds = $this->getCommonShopIdsCollectByUserType();

        $requestId = isset($request['id']) && $request['id'] ? $request['id'] : [];

        if($requestId){

            $ids = is_array($requestId) ? $requestId : explode(',', $requestId);

        }else{

            $ids = [];

        }

        $model = new PersonModel();

        $res          = $model->exportAdminPersonListResoure($shopIds,$request,$ids);

        return $this->success($res,'获取成功');

    }



    /**

     * 手动更新店铺信息

     * @return Json

     * @author ran

     * @date 2021-09-22 17:09

     * mailbox 466180170@qq.com

     */

    public function personHandel():Json

    {

        $model = new PersonModel();

        $res          = $model->personHandel();

        return $this->success($res,'获取成功');

    }

    /**
     * 正在营业门店列表
     */
    public function getAgentShopList()
    {
        $model = new PersonModel();

        $res          = $model->getAgentShopList();

        return $this->success($res,'获取成功');
    }
    /**
     * 代理店长列表
     */
    public function getAgentList()
    {
        $model = new PersonModel();

        $res          = $model->getAgentList();

        return $this->success($res,'获取成功');
    }
    /**
     * 修改代理店长下属门店
     */
    public function setShopAgent()
    {
        $model = new PersonModel();

        $res          = $model->setShopAgent($this->request->param());

        if($res)
        {
            return $this->success($res,'获取成功');
        }else{
            return resultArray(['error' => $model->getError()]);
        }
    }



}
